package com.drug.mingkang.permission.controller;

import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.permission.model.vo.PerUsersVo;
import com.drug.mingkang.permission.service.PerUsersService;
import com.drug.mingkang.utils.GiteeImgTools;
import com.drug.mingkang.utils.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

/**
 * 用户表(PerUsers)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:14
 */
@RestController
@RequestMapping("perUsers")
public class PerUsersController {

    /**
     * 服务对象
     */
    @Resource
    private PerUsersService perUsersService;

    @Resource
    private ResponseMessage responseMessage;


    /**
     * 删除所选用户ID
     * @param userIdAll
     * @return
     */
    @ApiOperation(value = "删除所选用户ID", notes = "输入用户id数组", httpMethod = "POST", response = ResponseMessage.class)
    @ResponseBody
    @PostMapping("/deleteUserIdAll")
    public ResponseMessage deleteUserIdAll(String[] userIdAll){
        for (String s : userIdAll) {
            System.out.println(s);
        }
        return perUsersService.deleteAllById(userIdAll);
    }

    /**
     *  根据用户ID修改状态
     * @param userId 用户ID
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "输入用户id", httpMethod = "POST", response = ResponseMessage.class)
    @ResponseBody
    @PostMapping("/deleteUser")
    public ResponseMessage deleteUser(Integer userId){
        return perUsersService.deleteById(userId);
    }

    /**
     * 增加用户 编辑用户
     * @param perUsers     用户对象封装
     * @param imgFile   图片
     * @param session   当前会话
     * @param role 角色数组id
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "增加用户 编辑用户", notes = "输入用户信息", httpMethod = "POST", response = ResponseMessage.class)
    @ResponseBody
    @PostMapping("/addOrrUpdateUser")
    public ResponseMessage addOrrUpdateUser(PerUsers perUsers, MultipartFile imgFile, Integer[] role, HttpSession session) throws Exception {
        return perUsersService.insertAndEdit(perUsers,imgFile,session,role);
    }



    /**
     * 审核用户
     * @param userId    用户ID
     * @param session   当前会话
     * @return
     */
    @ApiOperation(value = "审核用户", notes = "输入用户id修改状态", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("/auditUser")
    @ResponseBody
    public ResponseMessage auditUser(Integer userId, HttpSession session){
        PerUsers users = (PerUsers) session.getAttribute("perUsers");
        return perUsersService.auditUser(userId,  users.getUserName());
    }

    /**
     * 编辑或增加用户时判断用户名是否重复
     * @param userId    用户ID
     * @param userName  用户名
     * @return
     */
    @ApiOperation(value = "编辑或增加用户时判断用户名是否重复", notes = "输入用户名是否重复", httpMethod = "POST", response = ResponseMessage.class)
    @ResponseBody
    @PostMapping("/findUserName")
    public ResponseMessage findUserName(String userId,String userName){
        //判断是增加还是修改（修改不需要 名字更改不了）
        if(userId==null || "".equals(userId)){
            PerUsers perUsers = perUsersService.findUsers(userName);
            if(perUsers!=null){
                responseMessage.setStatus(1);
                responseMessage.setMessage("用户名已存在");
            }else{
                responseMessage.setStatus(0);
                responseMessage.setMessage("可以使用此用户名");
            }
        }else{
            responseMessage.setStatus(0);
        }
        return responseMessage;
    }


    /**
     * 本人修改基本信息
     * @param perUsers     用户对象封装
     * @param imgFile   图片
     * @param session   当前会话
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "本人修改基本信息", notes = "输入基本信息", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("/updateBasicInformation")
    @ResponseBody
    public ResponseMessage updateBasicInformation(PerUsers perUsers, MultipartFile imgFile, HttpSession session) throws Exception {
        return perUsersService.updateBasicInformation(perUsers, imgFile, session);
    }


    /**
     * 用户修改密码
     * @param oldPwd 旧密码
     * @param newPwd   新密码
     * @param session   当前会话
     * @return
     */
    @ApiOperation(value = "用户修改密码", notes = "输入新旧密码", httpMethod = "POST", response = PerUsers.class)
    @ResponseBody
    @PostMapping("/userUpdatePassword")
    public ResponseMessage userUpdatePassword(String oldPwd, String newPwd, HttpSession session){
        System.out.println("oldPwd = " + oldPwd);
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        if(!perUsers.getUserPassword().equals(oldPwd)){
            responseMessage.setStatus(2);
            responseMessage.setMessage("密码错误，请重新输入！");
            return responseMessage;
        }
        //设置新密码
        perUsers.setUserPassword(newPwd);
        return perUsersService.updatePassword(perUsers);
    }


    /**
     * 通过主键查询单条数据 附带角色id 数组
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/findByUsersId")
    @ResponseBody
    @ApiOperation(value = "通过主键查询单条数据", notes = "输入用户id", httpMethod = "POST", response = PerUsers.class)
    public PerUsers findByUsersId(Integer id) {
        return this.perUsersService.queryById(id);
    }


    /**
     * 显示所有用户
     * @param perUsersVo    实例对象
     * @return
     */
    @ApiOperation(value = "显示所有用户", notes = "输入查询条件", httpMethod = "POST", response = Map.class)
    @ResponseBody
    @PostMapping("/showListUser")
    public Map<String, Object> showListUser(PerUsersVo perUsersVo){
        //获取所有用户
        Map<String, Object>  map = perUsersService.queryAllByLimit(perUsersVo);
        return map;
    }
}